<?php

namespace app\api\controller;

use think\Controller;
use app\api\model\Zzrecord as ZzrecordModel;
use think\Db;


class Zzrecord extends Base
{
    //检查用户Token 前置操作
    // protected $beforeActionList = ['checkToken'];

    //记录信息
    public function index()
    {
        $id = input('id');
        $id = isset($id)?$id:3;
        $data = Db::name('zzrecord')->where('id','=',$id)->find();
        return self::apiResp(['_code' => 201, 'msg' => '成功','data' => $data]);
    }

    //添加修改记录
    public function add()
    {
        $res = (new ZzrecordModel())->addOrEdit();
        return self::apiResp($res);
    }

    //根据找人码查找信息
    public function find()
    {
        $code = input('code');
        $data = Db::name('zzrecord')->field('id,redpacket1,content,code')->where('code','=',$code)->find();
        if($data){
            return self::apiResp(['_code' => 201, 'msg' => '成功','data' => $data]);
        }else{
            return self::apiResp(['_code' => 500, 'msg' => '没有数据']);
        }
    }

    //根据发起人，推荐人，被推荐人查找记录
    public function find_2()
    {
        $field = input('field');
        $openid = input('openid');
        $data = Db::name('zzrecord')->where($field,$openid)->where('verif',2)->select();
        if($data){
            return self::apiResp(['_code' => 201, 'msg' => '成功','data' => $data]);
        }else{
            return self::apiResp(['_code' => 500, 'msg' => '没有数据']);
        }
    }

    //核销记录
    public function verif()
    {
        $id = input('id');
        $data['updatetime'] = time();
        $res = Db::name('zzrecord')->where('id',$id)->setField(['verif'=>'2','updatetime'=>$data['updatetime']]);
        if ($res > 0){
            //核销成功，修改用户的钱包
            $status = 0;
            DB::startTrans();
            try {
            $data = Db::name('zzrecord')->where('id',$id)->find();
            $res = Db::name('zz_user')->where('phone','=',$data['tuijianp'])->setField(['packet'=>$data['redpacket1']]);
            Db::name('zz_user')->where('phone','=',$data['beituijianp'])->setField(['packet'=>$data['redpacket2']]);
                Db::commit();
                $status = 1;
            } catch (\Exception $e) {
                Db::rollback();
                //throw $e;
            }
            if($status == 1){
                return self::apiResp(['_code' => 201, 'msg' => '成功!']);
            }else{
                return self::apiResp(['_code' => 500, 'msg' => '失败!']);
            }
        }else{
            return self::apiResp(['_code' => 500, 'msg' => '失败!']);
        } 
    }


    //提现请求，添加进提现记录表,并扣减用户的钱包余额
    public function addmoney()
    {
        $data = input();
        if($data['packet'] <= 0){
            return self::apiResp(['_code' => 500, 'msg' => '提现金额要大于0']);
        }
        $banlance = $data['packet'] - $data['money'];
        if($banlance < 0){
            return self::apiResp(['_code' => 500, 'msg' => '提现金额大于余额']);
        }
        $data['time'] = date('Y-m-d H:i:s');
        $info = Db::name('zz_user')->where('openid',$data['openid'])->find();
        $data['phone'] = $info['phone'];
        DB::startTrans();
            try {
                $ret = Db::name('zz_user')->where('openid',$data['openid'])->update(['packet'=>$banlance]);
                $res = Db::name('zzgetmoney')->insert($data);
                $id = Db::name('zzgetmoney')->getLastInsID();
                Db::commit();

            } catch (\Exception $e) {
                Db::rollback();
                //throw $e;
            }

        
        if ($res > 0)
            return self::apiResp(['_code' => 201, 'msg' => '提现提交已发送成功，静候审核','id' => $id]);
        else
            return self::apiResp(['_code' => 500, 'msg' => '提交失败!']);
    }




    //获取提现记录信息
    public function getmoney()
    {
        $openid = input('openid');
        $data = Db::name('zzgetmoney')->where('openid',$openid)->select();
        if($data){
            return self::apiResp(['_code' => 201, 'msg' => '成功','data' => $data]);
        }else{
            return self::apiResp(['_code' => 500, 'msg' => '没有数据']);
        }
        // return self::apiResp(['_code' => 201, 'msg' => '成功','data' => $data]);

    }


    //钱包有多少钱等
    public function packetinfo()
    {
        $openid = input('openid');
        $data = Db::name('zz_user')->where('openid',$openid)->find();
        if($data){
            return self::apiResp(['_code' => 201, 'msg' => '成功','data' => $data]);
        }else{
            return self::apiResp(['_code' => 500, 'msg' => '没有数据']);
        }
    
    }

    //有潜在消费者填信息，商家已充值的情况下，给三方发送服务进度通知
    public function send_1()
    {
        $id = input('id');
        //获取用户信息
        $info = Db::name('zzrecord')->where('id','=',$id)->find();
        
        // 获取模板信息
        $temp = Db::name('zztemplate')->where(['cate'=>1,'toobject'=>1,'use'=>1])->find();
        if($temp){
            $templateId = $temp['templateid'];
            $url = $temp['pagepath'];
        }else{
            return false;
        }
        
        $data = array(
         "first"  => $temp['title'],
         "remark" => $temp['remark'],
        );
        
        //给商家发送信息
        model('Zzrecord')->message($info['faqi_openid'],$templateId,$url,$data); 

        // 获取模板信息
        $temp = Db::name('zztemplate')->where(['cate'=>1,'toobject'=>2,'use'=>1])->find();
        if($temp){
            $templateId = $temp['templateid'];
            $url = $temp['pagepath'];
        }else{
            return false;
        }
        
        $data = array(
         "first"  => $temp['title'],
         "remark" => $temp['remark'],
        );
        //给推荐者发送
        model('Zzrecord')->message($info['tuijian_openid'],$templateId,$url,$data);

        // 获取模板信息
        $temp = Db::name('zztemplate')->where(['cate'=>1,'toobject'=>3,'use'=>1])->find();
        if($temp){
            $templateId = $temp['templateid'];
            $url = $temp['pagepath'];
        }else{
            return false;
        }
        
        $data = array(
         "first"  => $temp['title'],
         "remark" => $temp['remark'],
        );
        //给潜在消费者发送
        $res = model('Zzrecord')->message($info['beituijian_openid'],$templateId,$url,$data);

        if($res){
            return self::apiResp(['_code' => 201, 'msg' => '信息发送成功','data' => $res]);
        }else{
            return self::apiResp(['_code' => 500, 'msg' => '信息发送失败']);
        }
    }


    //给商家发送二次充值的信息，跳转二次充值的信息界面
    public function send_2()
    {
        $id = input('id');
        $info = Db::name('zzrecord')->where('id','=',$id)->find();
        // 获取模板信息
        $temp = Db::name('zztemplate')->where(['cate'=>3,'toobject'=>1,'use'=>1])->find();
        if($temp){
            $templateId = $temp['templateid'];
            $url = $temp['pagepath'];
        }else{
            return false;
        }
        
        $data = array(
         "first"  => $temp['title'],
         "remark" => $temp['remark'],
        );
        $res = model('Zzrecord')->message($data['faqip_openid'],$templateId,$url,$data);
        if($res){
            return self::apiResp(['_code' => 201, 'msg' => '信息发送成功','data' => $res]);
        }else{
            return self::apiResp(['_code' => 500, 'msg' => '信息发送失败']);
        }
    }

    //被推荐人点完成消费，给推荐者，商家发送核销提醒
    public function send_3()
    {
        $id = input('id');
        $info = Db::name('zzrecord')->where('id','=',$id)->find();
        // 获取模板信息
        $temp = Db::name('zztemplate')->where(['cate'=>4,'toobject'=>1,'use'=>1])->find();
        if($temp){
            $templateId = $temp['templateid'];
            $url = $temp['pagepath'];
        }else{
            return false;
        }
        
        $data = array(
         "first"  => $temp['title'],
         "remark" => $temp['remark'],
        );
        //给商家发送信信息
        $res = model('Zzrecord')->message($data['faqi_openid'],$templateId,$url,$data);

        // 获取模板信息
        $temp = Db::name('zztemplate')->where(['cate'=>4,'toobject'=>2,'use'=>1])->find();
        if($temp){
            $templateId = $temp['templateid'];
            $url = $temp['pagepath'];
        }else{
            return false;
        }
        
        $data = array(
         "first"  => $temp['title'],
         "remark" => $temp['remark'],
        );
        //给推荐者发送信信息
        $res = model('Zzrecord')->message($data['tuijian_openid'],$templateId,$url,$data);
        if($res){
            return self::apiResp(['_code' => 201, 'msg' => '信息发送成功','data' => $res]);
        }else{
            return self::apiResp(['_code' => 500, 'msg' => '信息发送失败']);
        }
    }

    //商家点击核销，给消费者，推荐者发送红包消息
    public function send_4()
    {
        $id = input('id');
        $info = Db::name('zzrecord')->where('id','=',$id)->find();
        // 获取模板信息
        $temp = Db::name('zztemplate')->where(['cate'=>3,'toobject'=>3,'use'=>1])->find();
        if($temp){
            $templateId = $temp['templateid'];
            $url = $temp['pagepath'];
        }else{
            return false;
        }
        
        $data = array(
         "first"  => $temp['title'],
         "remark" => $temp['remark'],
        );
        //给消费者发送信信息
        $res = model('Zzrecord')->message($data['beituijian_openid'],$templateId,$url,$data);

        // 获取模板信息
        $temp = Db::name('zztemplate')->where(['cate'=>3,'toobject'=>2,'use'=>1])->find();
        if($temp){
            $templateId = $temp['templateid'];
            $url = $temp['pagepath'];
        }else{
            return false;
        }
        
        $data = array(
         "first"  => $temp['title'],
         "remark" => $temp['remark'],
        );
        //给推荐者发送信信息
        $res = model('Zzrecord')->message($data['tuijian_openid'],$templateId,$url,$data);
        if($res){
            return self::apiResp(['_code' => 201, 'msg' => '信息发送成功','data' => $res]);
        }else{
            return self::apiResp(['_code' => 500, 'msg' => '信息发送失败']);
        }
    }

    //获取小程序码的接口
    public function qrcode()
    {
        $id = input('id');
        $res = Db::name('zzjump')->where('id',$id)->find();
        $re = $_SERVER['HTTP_HOST'].'/public/qrcode/'.$res['qrcode'];
        if($res){
            return self::apiResp(['_code' => 201, 'msg' => '成功','data' => $re]);
        }else{
            return self::apiResp(['_code' => 500, 'msg' => '失败']);
        }
    }


    //关于我们，简介的接口
    public function introduce()
    {
        $data = Db::name('zzintroduce')->where('use',1)->find();
        if($data){
            return self::apiResp(['_code' => 201, 'msg' => '成功','data' => $data]);
        }else{
            return self::apiResp(['_code' => 500, 'msg' => '没有数据']);
        }
    }
}